package com.orhon.smartcampus.modules.teacher.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.teacher.dao.AuthorizedPatentsDao;
import com.orhon.smartcampus.modules.teacher.entity.AuthorizedPatents;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLAuthorizedPatent;
import com.orhon.smartcampus.modules.teacher.mm.AuthorizedPatentMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AuthorizedPatentsFetcher implements DataFetcher  {

	@Autowired
	AuthorizedPatentsDao authorizedPatentsDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);
		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from AuthorizedPatents where deleted_at = null and id = "+id+" and school_id = " + schoolId;
		AuthorizedPatents item =  authorizedPatentsDao.findId(hql, null);
		if(item!=null) {
			GQLAuthorizedPatent mapEntity = AuthorizedPatentMapper.MAPPER.mapEntity(item);         
			return mapEntity;
		}
		return null;

	}
}
